<script setup lang="ts">
import { ref } from 'vue';

// OAuth2客户端配置
const clientId = '1001'; // 客户端ID
const redirectUri = window.location.origin + '/oauth/callback';
const responseType = 'code';
const scope = 'userinfo,profile'; // 请求的权限范围
const state = generateRandomState(); // 生成随机状态码，防止CSRF攻击

// 生成随机状态码
function generateRandomState() {
  return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
}

// 启动OAuth2授权流程
function startOAuth2Flow() {
  // 构建授权请求URL
  const authUrl = `/oauth2/authorize?client_id=${encodeURIComponent(clientId)}&response_type=${encodeURIComponent(responseType)}&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${encodeURIComponent(scope)}&state=${encodeURIComponent(state)}`;
  
  // 保存state到本地存储，以便回调时验证
  localStorage.setItem('oauth_state', state);
  
  // 跳转到授权页面
  window.location.href = authUrl;
}
</script>

<template>
  <div class="home">
    <div class="hero">
      <div class="hero-content">
        <h1>示例应用</h1>
        <p class="subtitle">使用OAuth2.0授权获取用户信息</p>
        
        <div class="auth-card">
          <div class="auth-header">
            <div class="auth-logo">🔐</div>
            <h2>第三方应用授权</h2>
          </div>
          
          <div class="auth-body">
            <p>点击下方按钮，通过OAuth2.0授权，获取您的基本信息。</p>
            
            <ul class="permission-list">
              <li>
                <span class="permission-icon">👤</span>
                <span class="permission-text">基本信息（用户名、昵称）</span>
              </li>
              <li>
                <span class="permission-icon">🏢</span>
                <span class="permission-text">部门信息</span>
              </li>
              <li>
                <span class="permission-icon">🔑</span>
                <span class="permission-text">角色和权限信息</span>
              </li>
            </ul>
            
            <button @click="startOAuth2Flow" class="oauth-button">
              <span class="button-icon">🔒</span>
              <span>授权登录</span>
            </button>
          </div>
          
          <div class="auth-footer">
            <p>本应用将通过安全的OAuth2.0协议获取授权，您可以随时取消或撤销授权。</p>
          </div>
        </div>
      </div>
    </div>
    
    <div class="features">
      <div class="feature">
        <div class="feature-icon">🔒</div>
        <h3>安全授权</h3>
        <p>通过OAuth2.0协议进行安全授权，无需分享密码。</p>
      </div>
      
      <div class="feature">
        <div class="feature-icon">🔄</div>
        <h3>完整流程</h3>
        <p>展示OAuth2.0授权码模式的完整流程实现。</p>
      </div>
      
      <div class="feature">
        <div class="feature-icon">👁️</div>
        <h3>透明可控</h3>
        <p>清晰展示授权范围，用户可随时取消授权。</p>
      </div>
    </div>
  </div>
</template>

<style scoped>
.home {
  font-family: 'Arial', sans-serif;
  color: #333;
}

.hero {
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
  min-height: 60vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  text-align: center;
}

.hero-content {
  max-width: 900px;
  width: 100%;
}

h1 {
  color: white;
  font-size: 36px;
  margin-bottom: 10px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.subtitle {
  color: rgba(255, 255, 255, 0.9);
  font-size: 18px;
  margin-bottom: 40px;
}

.auth-card {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  max-width: 500px;
  margin: 0 auto;
}

.auth-header {
  background-color: #f8f9fa;
  padding: 25px;
  border-bottom: 1px solid #e6e6e6;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.auth-logo {
  font-size: 36px;
  margin-bottom: 10px;
}

.auth-header h2 {
  margin: 0;
  color: #2c3e50;
  font-size: 22px;
}

.auth-body {
  padding: 30px;
}

.auth-body p {
  margin-top: 0;
  color: #555;
  font-size: 16px;
  line-height: 1.5;
}

.permission-list {
  list-style: none;
  padding: 0;
  margin: 25px 0;
  border: 1px solid #e6e6e6;
  border-radius: 6px;
  overflow: hidden;
}

.permission-list li {
  padding: 12px 15px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #e6e6e6;
  background-color: #f8f9fa;
}

.permission-list li:last-child {
  border-bottom: none;
}

.permission-icon {
  margin-right: 12px;
  font-size: 18px;
}

.permission-text {
  font-size: 14px;
  color: #333;
}

.oauth-button {
  background-color: #4285f4;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 14px 25px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  margin-top: 20px;
  transition: background-color 0.3s;
}

.oauth-button:hover {
  background-color: #357ae8;
}

.button-icon {
  margin-right: 8px;
  font-size: 18px;
}

.auth-footer {
  padding: 15px 30px;
  background-color: #f8f9fa;
  border-top: 1px solid #e6e6e6;
}

.auth-footer p {
  margin: 0;
  color: #666;
  font-size: 12px;
  text-align: center;
}

.features {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  padding: 60px 20px;
  background-color: #f5f8fa;
}

.feature {
  flex: 1;
  min-width: 250px;
  max-width: 300px;
  margin: 20px;
  text-align: center;
  padding: 30px 20px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s, box-shadow 0.3s;
}

.feature:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1);
}

.feature-icon {
  font-size: 40px;
  margin-bottom: 15px;
}

.feature h3 {
  color: #2c3e50;
  margin-top: 0;
  font-size: 18px;
}

.feature p {
  color: #666;
  font-size: 14px;
  line-height: 1.6;
}
</style> 